import configparser
import random
import string
from datetime import datetime


def read_config(file_name):
    config = configparser.ConfigParser()
    if not config.read(file_name):
        return 10000
    return int(config.get('Settings', 'quantity', fallback='10000'))


def generate_codes(quantity):
    codes = set()
    while len(codes) < quantity:
        code = ''.join(random.choices(string.ascii_uppercase + string.digits, k=10))
        codes.add(code)
    return codes


def save_to_file(codes, file_name):
    with open(file_name, 'w', encoding='utf-8') as file:
        for code in codes:
            file.write(code + '\n')


def main():
    quantity = read_config('settings.ini')
    codes = generate_codes(quantity)
    timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
    file_name = f'codes_{timestamp}.txt'
    save_to_file(codes, file_name)


if __name__ == '__main__':
    main()
